import { useAutoSave } from '@/composables/useAutoSave';

export function useArticleAutoSave(
  form: any,
  saveFn: (_data: any) => void,
  interval = 30000
) {
  return useAutoSave(
    form,
    () => {
      let content = '';
      if (form.content_type === 'markdown' || form.content_type === 1) {
        content = form.articleContent || '';
      } else {
        const html = form.editorValue || '';
        content = html.replace(/<[^>]+>/g, '').replace(/\s/g, '');
      }
      if (!form.title.trim() && (!content || content === '')) {
        return;
      }
      saveFn({
        ...form,
        content:
          form.content_type === 'markdown' || form.content_type === 1
            ? form.articleContent
            : form.editorValue
      });
    },
    interval
  );
}
